package com.manli.api.service;

import com.manli.api.pojo.QueryParamsActivity;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.ActivityTypeDao;
import com.manli.api.dto.ActivityType;


@Service("activityTypeService")
public class ActivityTypeService {
    @Autowired
    ActivityTypeDao activityTypeDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParamsActivity params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from activity_type where 1=1 ");
        sql.append(params.getStatus() == null?"":" and status = " + params.getStatus());
        sql.append(StringUtils.isEmpty(params.getActivityTypeName())?"":" and activity_type_name like '%" + params.getActivityTypeName() + "%'");
        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<ActivityType> pageQuery = new PageQuery<ActivityType>(params.getPage(),params.getLimit());
        PageQuery<ActivityType> query =sqlManager.execute(sqlReady,ActivityType.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<ActivityType> list = activityTypeDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer activityTypeId) {
        ActivityType activityType = activityTypeDao.single(activityTypeId);
        return ResultUtil.success(activityType);
    }

    @Transactional
    public Result save(ActivityType activityType) {
        if(StringUtils.isEmpty(activityType.getActivityTypeName())){
            return ResultUtil.error(-1,"名称为空");
        }
        if(activityType.getStatus() == null){
            return ResultUtil.error(-1,"状态为空");
        }
        activityTypeDao.insert(activityType);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(ActivityType activityType) {
        if(StringUtils.isEmpty(activityType.getActivityTypeName())){
            return ResultUtil.error(-1,"名称为空");
        }
        if(activityType.getStatus() == null){
            return ResultUtil.error(-1,"状态为空");
        }
        if(activityType.getActivityTypeId() == null){
            return ResultUtil.error(-1,"id为空");
        }
        activityTypeDao.updateTemplateById(activityType);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(ActivityType activityType) {
        activityTypeDao.updateTemplateById(activityType);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            activityTypeDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
